<template>
  <div class="login-panel-position">

    <el-card class="box-card el-card-position">

      <div slot="header" class="clearfix span-title">
        <span>因为懂你 猜你喜欢\(^o^)/~</span>
      </div>

      <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="60px" class="demo-ruleForm">
        <el-form-item label="用户ID" prop="userid">
          <el-input v-model.number="ruleForm.userid"></el-input>
        </el-form-item>

        <el-form-item label="密码" prop="pass">
          <el-input type="password" v-model="ruleForm.pass" autocomplete="on"></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>

    </el-card>
  </div>
</template>

<script>
  export default {
    data() {

      var checkUserId = (rule, value, callback) => {
        if (!value) {
          return callback(new Error('用户ID不能为空'));
        }
        callback();
      };
      var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        }
        callback();
      };

      return {
        ruleForm: {
          pass: '',
          userid: ''
        },
        rules: {
          pass: [{
            validator: validatePass,
            trigger: 'blur'
          }],
          userid: [{
            validator: checkUserId,
            trigger: 'blur'
          }]
        }
      };
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            let _this = this;
            this.$axios.post("/api/login", _this.ruleForm).then(response => {
              if (response.data.result) {
                this.$message.success('登录成功！');
                // this.$refs[formName].resetFields();
                setTimeout(() => {
                  this.$router.push({
                    path: '/cars'
                  });
                }, 500);
              } else {
                this.$message.error(response.data.msg);
              }
            });
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    }
  }
</script>

<style scoped>
  .span-title {
    color: #00b8ff;
    font-size: large;
  }

  .el-card-position {
    margin-top: 150px;
  }

  .login-panel-position {
    width: 30%;
    margin-left: 35%;
  }
</style>
